package com.wt.struggle.goodbye.user.adapter

import android.content.Context
import android.os.Bundle
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ProBaseAdapter
import com.wt.struggle.goodbye.info.NewsInfo
import com.wt.struggle.goodbye.info.ProUserInfo
import com.wt.struggle.goodbye.user.activity.FansActivity
import com.wt.struggle.goodbye.user.activity.FocusOnActivity
import com.xin.lv.yang.utils.utils.ImageUtil
import kotlinx.android.synthetic.main.fans_item.view.*

class FansAdapter(context: Context, list: ArrayList<ProUserInfo>) : ProBaseAdapter<ProUserInfo>(context, list) {

    var joupCode = 0

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val vh = holder as VH
        val info = list[position]

        show(vh, info)

    }

    override fun onCreateView(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = inflater.inflate(R.layout.fans_item, parent, false)
        return VH(view)
    }

    override fun onUpdateHolder(holder: RecyclerView.ViewHolder, position: Int, payloads: MutableList<Any>) {
        val bundle = payloads[0] as Bundle
        val set = bundle.keySet()
        for (temp in set) {
            when (temp) {
                "info" -> {
                    val info: ProUserInfo = bundle.getParcelable(temp)
                    show(holder as VH, info)
                }
            }
        }
    }


    fun show(vh: VH, info: ProUserInfo) {

        ImageUtil.getInstance()
            .loadCircleCropImage(context, vh.imageFansHead, Config.GET_IMAGE_URL + info.avatar, R.mipmap.logo_small)

        vh.tvFanName.text = info.nickName
        vh.tvFansPhone.text = info.mobile

        if (joupCode == 1) {

            vh.imageAttention.setImageResource(R.drawable.coll_n)
            vh.tvAttention.text = "已关注"
            vh.tvAttention.setTextColor(context.resources.getColor(R.color.gray))

        } else {

            if (info.isCollection == 1) {
                vh.imageAttention.setImageResource(R.drawable.coll_n)
                vh.tvAttention.text = "已关注"
                vh.tvAttention.setTextColor(context.resources.getColor(R.color.gray))
                vh.attentionLinear.isClickable = false
            } else {
                vh.imageAttention.setImageResource(R.drawable.coll_y)
                vh.tvAttention.text = "关注"
                vh.attentionLinear.isClickable = true
            }
        }


        vh.attentionLinear.setOnClickListener {
            if (context is FansActivity) {
                (context as FansActivity).collect(vh.adapterPosition)
            } else if (context is FocusOnActivity) {
                (context as FocusOnActivity).collect(vh.adapterPosition)
            }

        }

    }

    class VH(view: View) : RecyclerView.ViewHolder(view) {
        val imageFansHead = view.imageFansHead
        val tvFanName = view.tvFanName
        val tvFansPhone = view.tvFansPhone
        val attentionLinear = view.attentionLinear
        val imageAttention = view.imageAttention
        val tvAttention = view.tvAttention
    }
}